//
//  ArtGalleryCollectionCell.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/21.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "ArtGalleryCollectionCell.h"
#import "ArtGalleryCell_third.h"
#import "FindLayout.h"
#import "IGDatabaseApi.h"
#import "XiangQingViewController.h"
@interface ArtGalleryCollectionCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
}

@end
@implementation ArtGalleryCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self loadUI];
//        self.dataSource = [@[] mutableCopy];
    }
    return self;
}

- (void)setDataSource:(NSMutableArray *)dataSource
{
    if (self.dataSource != dataSource)
    {
        _dataSource = dataSource;
        [self.collectionView reloadData];
    }
}

- (void)loadUI
{
    FindLayout *layout = [[FindLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.scrollEnabled = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor c3Color];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ArtGalleryCell_third class] forCellWithReuseIdentifier:@"ArtGalleryCell_third"];
    [self addSubview:self.collectionView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.contentView.bounds;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count ? self.dataSource.count : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ArtGalleryCell_third *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_third" forIndexPath:indexPath];
    IGArtWorkModel *base = _dataSource[indexPath.item];
    cell.imgUrl = base.image;
    cell.titleL = base.title;
    cell.isTreasure = [base.isMarked isEqualToString:@"1"] ? YES : NO;
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataSource[indexPath.item];
    XiangQingViewController *vc = [[XiangQingViewController alloc] init];
    vc.p_id = base.aId;
    vc.isRoot = YES;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataSource[indexPath.item];
    if ([base.plates integerValue]==1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

@end
